package com.yohann.course_service.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yohann.course_service.entity.Chapter;
import com.yohann.course_service.entity.Video;
import com.yohann.course_service.entity.vo.ChapterInfo;
import com.yohann.course_service.mapper.ChapterMapper;
import com.yohann.course_service.service.ChapterService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yohann.course_service.service.VideoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 章节 服务实现类
 * </p>
 *
 * @author Yohann
 * @since 2020-10-18
 */
@Service
public class ChapterServiceImpl extends ServiceImpl<ChapterMapper, Chapter> implements ChapterService {
    @Autowired
    private VideoService videoService;

    /**
     * 根据id查询章节信息
     *
     * @param id
     * @return
     */
    @Override
    public ChapterInfo getChapterInfoById(int id) {
        //查询章节信息
        Chapter chapter = baseMapper.selectById(id);

        //查询章节对应的小节信息
        QueryWrapper<Video> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(Video::getChapterId, id);
        List<Video> videoList = videoService.list(queryWrapper);

        //设置视图对象信息
        ChapterInfo chapterInfo = new ChapterInfo();
        chapterInfo.setId(chapter.getId())
                .setCourseId(chapter.getCourseId())
                .setTitle(chapter.getTitle())
                .setSort(chapter.getSort())
                .setVideos(videoList);

        return chapterInfo;
    }

    /**
     * 查询课程的章节信息
     *
     * @param courseId
     * @return
     */
    @Override
    public List<ChapterInfo> getChapterInfo(int courseId) {
        //通过课程id查询对应的章节信息 根据sort升序排列
        LambdaQueryWrapper<Chapter> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Chapter::getCourseId, courseId).orderByAsc(Chapter::getSort);

        List<Chapter> chapters = baseMapper.selectList(queryWrapper);

        //遍历章节信息 查询对应的小节信息
        List<ChapterInfo> chapterInfoList = new ArrayList<>();
        ChapterInfo chapterInfo;
        for (Chapter chapter : chapters) {
            //查询对应小节信息
            LambdaQueryWrapper<Video> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(Video::getChapterId, chapter.getId()).orderByAsc(Video::getSort);

            List<Video> videoList = videoService.list(wrapper);

            //设置视图对象信息
            chapterInfo = new ChapterInfo();
            chapterInfo.setId(chapter.getId())
                    .setCourseId(chapter.getCourseId())
                    .setTitle(chapter.getTitle())
                    .setSort(chapter.getSort())
                    .setVideos(videoList);

            //添加到章节视图信息集合
            chapterInfoList.add(chapterInfo);
        }

        return chapterInfoList;
    }

    /**
     * 删除章节
     *
     * @param id
     * @return
     */
    @Override
    @Transactional(rollbackFor = {Exception.class})
    public void deleteChapter(int id) {
        //删除小节信息
        videoService.removeAllVideo(id);

        //删除章节信息
        this.removeById(id);
    }
}
